package com.example;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SQLLiteActivity extends Activity {

    private Button createDB = null;
    private Button updateDB = null;
    private Button insert = null;
    private Button update = null;
    private Button query = null;

    private MySQLiteManager mySQLiteManager = null;

    private SQLiteDatabase sqLiteDatabase;

    public SQLLiteActivity() {
        this.init();
    }

    private void init() {
        this.mySQLiteManager = new MySQLiteManager(SQLLiteActivity.this, "SQLite01");
    }

    /**
     * 初始化必须在setContentView之后
     */
    private void initWidgets() {
        this.createDB = (Button) findViewById(R.id.createDatabase);
        this.updateDB = (Button) findViewById(R.id.updateDatabase);
        this.insert = (Button) findViewById(R.id.insert);
        this.query = (Button) findViewById(R.id.query);
        this.update = (Button) findViewById(R.id.update);

        return;
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        this.initWidgets();

        this.createDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sqLiteDatabase = mySQLiteManager.getWritableDatabase();
//                sqLiteDatabase.execSQL("drop table B");
                sqLiteDatabase.execSQL("Create table B(id int,name varchar(20))");
                System.out.println("Table is created...");
            }
        });

        this.updateDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("..................... will update database...............");
                mySQLiteManager = new MySQLiteManager(SQLLiteActivity.this, "SQLite01", 4);
            }
        });

        this.insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("id", 10);
                contentValues.put("name", "Lisi");

                sqLiteDatabase.insert("b", null, contentValues);
                System.out.println("Had inserted the value into the table B");

            }
        });

        this.update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sqLiteDatabase.execSQL("update b set name='zhang san' where id=1");
                System.out.println("Had update table b...");


            }
        });
    }


}